package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.HistoryKeywordVo;
import com.cskaoyan.bean.vo.KeywordAndSearchHistoryVo;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.mapper.SearchHistoryMapper;
import com.cskaoyan.service.SearchService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description
 * 搜索接口
 *
 * @author Zhangfei
 * @date Created on 2021/10/21
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SearchServiceImpl implements SearchService {
    @Autowired
    KeywordMapper keywordMapper;


    @Autowired
    SearchHistoryMapper searchHistoryMapper;


    /**
     * 根据输入返回关键词
     *
     * @param keyword
     * @return
     */
    @Override
    public List<String> helper(String keyword) {
        //根据条件查询
        List<String> list = keywordMapper.selectKeywordByKeyword(keyword);
        return list;
    }

    /**
     * 查询默认关键词，热点关键词
     * 还要搜索历史
     *
     * @return
     */
    @Override
    public KeywordAndSearchHistoryVo index() {
        //查询关键词集合
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        //执行
        List<Keyword> hotKeywordList = keywordMapper.selectByExample(keywordExample);
        Keyword defaultKeyword = null;
        //遍历查找默认关键词
        for (Keyword keyword : hotKeywordList) {
            if (keyword.getIsDefault()) {
                defaultKeyword = keyword;
                break;
            }
        }
        //查询搜索历史
        List<HistoryKeywordVo> historyKeywordList = null;
        //获得登陆主体
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (user != null) {
            //非null则说明已经登录
            //传入id
            Integer userId = user.getId();
            historyKeywordList = searchHistoryMapper.selectHistoryKeywordByUserId(userId);
        }
        //赋值
        KeywordAndSearchHistoryVo keywordAndSearchHistoryVo = new KeywordAndSearchHistoryVo();
        keywordAndSearchHistoryVo.setDefaultKeyword(defaultKeyword);
        keywordAndSearchHistoryVo.setHotKeywordList(hotKeywordList);
        keywordAndSearchHistoryVo.setHistoryKeywordList(historyKeywordList);

        //返回值
        return keywordAndSearchHistoryVo;
    }


    /**
     * 根据用户id
     * 删除登录用户的历史搜素记录
     *
     * @return
     */
    @Override
    public Integer clearhistory(Integer userId) {
        //加入条件
        SearchHistoryExample example = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setDeleted(true);
        //执行
        int count = searchHistoryMapper.updateByExampleSelective(searchHistory, example);
        return count;
    }
}
